import request from '@/utils/request'
import axios from "axios";
const baseUrl = process.env.VUE_APP_BASE_API

export function exportExcel(data, type = 'application/vnd.ms-excel') {
    let params = "";
    if (data.params) {
        for (let key in data.params) {
            params += "" + key + "=" + data.params[key] + "&";
        }
        params = params.substring(0, params.length - 1);
    }
    return request({
        method: data.method,
        url: `${baseUrl}${data.url}${params ? '?' + params : ''}`,
        responseType: 'blob'
    }).then((res) => {
        const link = document.createElement('a')
        let blob = new Blob([res], {
            type
        })
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
            // link.download = res.headers['content-disposition'] //下载后文件名
        link.download = data.fileName //下载的文件名
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
    }).catch(error => {
        this.$message.error({
            title: '错误',
            desc: '网络连接错误'
        })
        console.log(error)
    })
}

export function uuid() {
    var temp_url = URL.createObjectURL(new Blob());
    var uuid = temp_url.toString(); // blob:https://xxx.com/b250d159-e1b6-4a87-9002-885d90033be3
    URL.revokeObjectURL(temp_url);
    return uuid.substr(uuid.lastIndexOf("/") + 1);
}

// 延时调用
export function debounce(fn, t) {
    let delay = t || 200;
    let timer = null;
    return function() {
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            fn.apply(this, arguments);
        }, delay);
    }
}


export function bFormatterTo(size) {
    if (!size)
        return "";
    const num = 1024.00; //byte
    if (size < num)
        return size + "B";
    if (size < Math.pow(num, 2))
        return (size / num).toFixed(2) + "K"; //kb
    if (size < Math.pow(num, 3))
        return (size / Math.pow(num, 2)).toFixed(2) + "M"; //M
    if (size < Math.pow(num, 4))
        return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G
    return (size / Math.pow(num, 4)).toFixed(2) + "T"; //T
}

export function getDuration(my_time) {
    var days = my_time / 1000 / 60 / 60 / 24;
    var daysRound = Math.floor(days);
    var hours = my_time / 1000 / 60 / 60 - (24 * daysRound);
    var hoursRound = Math.floor(hours);
    var minutes = my_time / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
    var minutesRound = Math.floor(minutes);
    var seconds = my_time / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
    seconds = seconds.toFixed(2)
    const time = `${daysRound}天${hoursRound}时${minutesRound}分${seconds}秒`
    return time;
}


export function timeSlot(step, hour) { //  step = 间隔的分钟
    var date = new Date()
    date.setHours(hour ? hour : '00') // 时分秒设置从零点开始
    date.setSeconds('00')
    date.setUTCMinutes('00')
    var arr = [],
        timeArr = [];
    var slotNum = 24 * 60 * 60 / step // 算出多少个间隔
    for (var f = 0; f < slotNum; f++) { //  stepM * f = 24H*60M
        // arr.push(new Date(Number(date.getTime()) + Number(step*60*1000*f)))   //  标准时间数组
        var time = new Date(Number(date.getTime()) + Number(step * 1000 * f)) // 获取：零点的时间 + 每次递增的时间
        var hour = '',
            minute = '',
            sec = '';
        time.getHours() < 10 ? hour = '0' + time.getHours() : hour = time.getHours() // 获取小时
        time.getMinutes() < 10 ? minute = '0' + time.getMinutes() : minute = time.getMinutes() // 获取分钟
        time.getSeconds() < 10 ? sec = '0' + time.getSeconds() : sec = time.getSeconds() // 获取秒钟
        timeArr.push(hour + ':' + minute + ':' + sec)
    }
    return timeArr
}
// console.log(timeSlot(5))   // 传入参数：每间隔5秒钟